package com.google.zxing.ohos;

import com.google.zxing.client.ohos.InactivityTimer;
import ohos.app.Context;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.event.commonevent.MatchingSkills;
import ohos.eventhandler.EventHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.*;

@PrepareForTest({InactivityTimer.class, CommonEventSubscribeInfo.class})
@RunWith(PowerMockRunner.class)
public class InactivityTimerTestOs {
    InactivityTimer inactivityTimer;

    @Before
    public void setUp() throws Exception {
        Context context = mock(Context.class);
        MatchingSkills matchingSkills = mock(MatchingSkills.class);
        whenNew(MatchingSkills.class).withNoArguments().thenReturn(matchingSkills);
        CommonEventSubscribeInfo eventSubscribeInfo = mock(CommonEventSubscribeInfo.class);
        whenNew(CommonEventSubscribeInfo.class).withArguments(matchingSkills).thenReturn(eventSubscribeInfo);
        whenNew(CommonEventSubscriber.class).withArguments(eventSubscribeInfo).thenReturn(mock(CommonEventSubscriber.class));


        inactivityTimer = new InactivityTimer(context, new Runnable() {
            @Override
            public void run() {

            }
        });


    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void activity() throws Exception {
        EventHandler handler = mock(EventHandler.class);
        whenNew(EventHandler.class).withAnyArguments().thenReturn(handler);
        PowerMockito.field(InactivityTimer.class, "onBattery").set(inactivityTimer, true);
        inactivityTimer.activity();
        verify(handler, times(1)).postTask(any(), 5 * 60 * 1000L);
    }

    @Test
    public void start() throws Exception {
        EventHandler handler = mock(EventHandler.class);
        whenNew(EventHandler.class).withAnyArguments().thenReturn(handler);
        inactivityTimer.start();
        verify(handler, times(1)).postTask(any(), 5 * 60 * 1000L);
    }

    @Test
    public void cancel() {
        inactivityTimer.cancel();
        boolean registered = Whitebox.getInternalState(inactivityTimer, "registered");
        assertEquals(false, registered);
    }
}